package com.heima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.mapper.ChannelMapper;
import com.heima.mapper.SkuMapper;
import com.heima.mapper.VMMapper;
import com.heima.pojo.Channel;
import com.heima.pojo.Sku;
import com.heima.pojo.VendingMachine;
import com.heima.pojo.dto.ChannelDto;
import com.heima.pojo.dto.ChannelListDto;
import com.heima.pojo.vo.ChannelVo;
import com.heima.service.ChannelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
@Transactional
public class ChannelServiceImpl implements ChannelService {
    @Autowired
    private ChannelMapper channelMapper;
    @Autowired
    private VMMapper vmMapper;
    @Autowired
    private SkuMapper skuMapper;

    @Override
    public List<ChannelVo> channelInfo(String innerCode) {
        List<ChannelVo> channelVo = channelMapper.getChannelInfo(innerCode);
        return channelVo;
    }

    @Transactional
    @Override
    public Boolean channelConfig(ChannelDto channelDto) {
        // 1. 判断该设备是否存在
        VendingMachine vendingMachine = vmMapper.selectOne(new QueryWrapper<VendingMachine>().eq("inner_code", channelDto.getInnerCode()));
        if (vendingMachine == null) {
            throw new RuntimeException("设备不存在");
        }

        // 2. 获取根据货道的channelCode判断该货道是否有商品
        List<ChannelListDto> channelInfo = channelDto.getChannelList();
        for (ChannelListDto channelListDto : channelInfo) {
            // 根据货道的channelCode和innerCode判断该货道是否存在
            LambdaQueryWrapper<Channel> wrapperChannel = Wrappers.<Channel>lambdaQuery()
                    .eq(Channel::getChannelCode, channelListDto.getChannelCode())
                    .eq(Channel::getInnerCode, channelDto.getInnerCode());
            Channel channel = channelMapper.selectOne(wrapperChannel);

            // 如果货道不存在，记录日志并返回false
            if (channel == null) {
                log.error("货道 {} 不存在", channelListDto.getChannelCode());
                return false;  // 货道不存在时返回失败
            }

            // 获取 SKU 信息
            Long skuId = Long.parseLong(channelListDto.getSkuId());
            Sku sku = skuMapper.selectById(skuId);

            // 如果 SKU 不存在，记录日志并返回false
            if (sku == null) {
                log.error("skuId:{} 不存在", channelListDto.getSkuId());
                return false;  // 商品不存在时返回失败
            }

            // 更新货道的 skuId
            channel.setSkuId(sku.getSkuId());

            // 执行数据库更新操作
            int updateResult = channelMapper.updateById(channel);

            // 如果更新失败，记录日志并返回false
            if (updateResult == 0) {
                log.error("更新货道 {} 失败", channelListDto.getChannelCode());
                return false;  // 更新失败时返回失败
            }
        }

        // 所有货道更新成功，返回true
        return true;
    }

}
